package com.imagecollector.common.handler;

import com.imagecollector.common.R;
import com.imagecollector.common.enums.CodeEnum;
import com.imagecollector.common.exception.BaseException;
import com.imagecollector.common.exception.InvalidInputException;
import com.imagecollector.common.exception.TencentServiceException;
import com.imagecollector.common.exception.UnauthorizedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolationException;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author: wrf
 * @Date: 2024/6/26 8:43
 * @Description: Controller异常处理类
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {BaseException.class})
    public R handleBaseException(BaseException e) {
        log.error("业务异常:", e);
        return R.fail(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(value = {MissingServletRequestParameterException.class})
    public R handleMethodArgumentNotValidException(MissingServletRequestParameterException e) {
        log.error("参数校验失败:", e);
        return R.fail(CodeEnum.PARAM_ERROR);
    }

    @ExceptionHandler(value = {NullPointerException.class})
    public R handleNullPointerException(NullPointerException e) {
        log.error(e.getMessage(), e);
        return R.fail(CodeEnum.FAIL.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = {ConstraintViolationException.class})
    public R handleConstraintViolationException(ConstraintViolationException e) {
        log.error(e.getMessage(), e);
        return R.fail().msg("参数条件不满足");
    }

    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        return R.fail().msg(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
    }


    @ExceptionHandler(value = InvalidInputException.class)
    public R handleParamException(InvalidInputException e) {
        log.error(e.getMessage(), e);
        return R.fail(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(value = {OptimisticLockingFailureException.class})
    public R handleOptimisticLockingFailureException(OptimisticLockingFailureException e) {
        log.error(e.getMessage(), e);
        return R.fail(CodeEnum.FAIL.getCode(), "数据已被修改，请刷新后重试");
    }

    @ExceptionHandler(value = UnauthorizedException.class)
    public R handleAuthException(UnauthorizedException e) {
        log.error(e.getMessage(), e);
        return R.fail(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(value = TencentServiceException.class)
    public R handleTencentServiceException(TencentServiceException e) {
        log.error(e.getMessage(), e);
        return R.fail(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(value = IOException.class)
    public R handleIOException(IOException e) {
        log.error(e.getMessage(), e);
        return R.fail(CodeEnum.FAIL.getCode(), e.getMessage());
    }

    /**
     * 处理所有 Exception 异常
     */
    @ExceptionHandler({Exception.class})
    public R handleAllException(Exception e) {
        log.error(e.getMessage(), e);
        return R.fail(CodeEnum.SERVER_ERROR);
    }
}
